// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
angular.module('starter', ['ionic', 'starter.controller', 'oc.lazyLoad', 'ionic-material', 'common.services'])


    .run(function ($ionicPlatform, $localstorage, $http) {
        $ionicPlatform.ready(function () {
            // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
            // for form inputs)

            if (window.cordova && window.cordova.plugins.Keyboard) {
                cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
            }
            if (window.StatusBar) {
                StatusBar.styleDefault();
            }
            $http.get('http://xuhuatang.com/huatang/public/getWeiUserInfo').success(function (data) {
                $localstorage.setObject('weiInfo', data.data);
            });
        });
    })

//add lzayload config;
    .constant('JQ_CONFIG', {})

    .constant('API', {
        baseURL: 'http://xuhuatang.com/huatang/public/'
        //baseURL: 'http://localhost/huatang/public/'
    })

    .config(function ($stateProvider, $ionicConfigProvider, $urlRouterProvider, JQ_CONFIG, $ocLazyLoadProvider) {
        $stateProvider

            .state('app', {
                url: '/app',
                abstract: true,
                templateUrl: 'templates/menu.html',
                controller: 'AppCtrl'
            })

            .state('app.home', {
                url: '/home',
                views: {
                    'menuContent': {
                        templateUrl: 'templates/home.html',
                        controller: 'HomeCtrl',
                        resolve: load(['js/controllers/home.js?v=1.1'])
                    }
                },

            })

            .state('app.lists', {
                url: '/lists',
                views: {
                    'menuContent': {
                        templateUrl: 'templates/lists.html',
                        controller: 'ListsCtrl',
                        resolve: load(['js/controllers/ListsCtrl.js'])

                    }
                }
            })

            .state('app.ink', {
                url: '/ink',
                views: {
                    'menuContent': {
                        templateUrl: 'templates/ink.html',
                        controller: 'InkCtrl',
                        resolve: load(['js/controllers/InkCtrl.js'])

                    }
                }
            })

            .state('app.motion', {
                url: '/motion',
                views: {
                    'menuContent': {
                        templateUrl: 'templates/motion.html',
                        controller: 'MotionCtrl',
                        resolve: load(['js/controllers/MotionCtrl.js'])
                    }
                }
            })

            .state('app.components', {
                url: '/components',
                views: {
                    'menuContent': {
                        templateUrl: 'templates/components.html',
                        controller: 'ComponentsCtrl',
                        resolve: load(['js/controllers/ComponentsCtrl.js'])
                    }
                }
            })

            .state('app.extensions', {
                url: '/extensions',
                views: {
                    'menuContent': {
                        templateUrl: 'templates/extensions.html',
                        controller: 'ExtensionsCtrl',
                        resolve: load(['js/controllers/ExtensionsCtrl.js'])
                    }
                }
            })

            .state('app.editUserInfo', {
                url: '/editUserInfo',
                views: {
                    'menuContent': {
                        templateUrl: 'templates/editUserInfo.html',
                        controller: 'EditUserInfoCtrl',
                        resolve: load(['js/controllers/editUserInfo.js'])
                    }
                }
            })
        ;

        // if none of the above states are matched, use this as the fallback
        $urlRouterProvider.otherwise('/app/home');

        function load(srcs, callback) {
            return {
                deps: ['$ocLazyLoad', '$q',
                    function ($ocLazyLoad, $q) {
                        var deferred = $q.defer();
                        var promise = false;
                        srcs = angular.isArray(srcs) ? srcs : srcs.split(/\s+/);
                        if (!promise) {
                            promise = deferred.promise;
                        }
                        angular.forEach(srcs, function (src) {
                            promise = promise.then(function () {
                                if (JQ_CONFIG[src]) {
                                    return $ocLazyLoad.load(JQ_CONFIG[src]);
                                }
                                name = src;
                                return $ocLazyLoad.load(name);
                            });
                        });
                        deferred.resolve();
                        return callback ? promise.then(function () {
                            return callback();
                        }) : promise;
                    }]
            }
        }
    });
